package com.me.batm.screens;

import java.util.Vector;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.me.batm.Button;
import com.me.batm.GameConstants;
import com.me.batm.OurGdxGame;
import com.me.batm.services.GameConfiguration;

public class MenuScreen implements Screen{
	OurGdxGame ourGdxGame;
	private Stage stage;		
	
	Label nameLabel;	
	TextField nameText;
	private AssetManager _assetManager = null;
	private TextureRegion _imgMouse = null;
	private Vector3 _mousePos = null;
	private SpriteBatch _batch = null;
	private OrthographicCamera _camera = null;
	private BitmapFont _fontText;

	Long timeLoading;
	
	//GUI Buttons
	Button startGame;
	Button howPlayGame;
	Button quitGame;
	
	private TextureRegion imgBackground;
	
	boolean isLoading = false;
	protected Label titleScreen;
	static int BUTTON_WITH = 250;
	
	public MenuScreen(OurGdxGame _ourGdxGame) {
		this.ourGdxGame = _ourGdxGame;
		stage = new Stage();
		Gdx.input.setInputProcessor(stage);		
		imgBackground = null;

		startGame = new Button(this.ourGdxGame, GameConstants.DESKTOP_SCREEN_WIDTH/2, 
				(int) (GameConstants.DESKTOP_SCREEN_HEIGHT*0.5), "Start Game");
		howPlayGame = new Button(this.ourGdxGame, GameConstants.DESKTOP_SCREEN_WIDTH/2, 
				(int) (GameConstants.DESKTOP_SCREEN_HEIGHT*0.5) + 80, "How To Play?");
		quitGame = new Button(this.ourGdxGame, GameConstants.DESKTOP_SCREEN_WIDTH/2, 
				(int) (GameConstants.DESKTOP_SCREEN_HEIGHT*0.5) + 160, "Quit");
		
		_fontText = new BitmapFont(Gdx.files.internal("data/normalFont.fnt"),
		         Gdx.files.internal("data/normalFont.png"), true);		
	}
	
	public void displayScreen() {
		//ourGdxGame.setScreen(new MainScreen(this.ourGdxGame, nameText.getText())); 
	}
	
	@Override
	public void render(float delta) {		
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);		
		if(isLoading) {
			Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
	        stage.act(Gdx.graphics.getDeltaTime());
	        stage.draw();
	        Table.drawDebug(stage);     
	       
			if(System.currentTimeMillis() - timeLoading > 500) {
				//ourGdxGame.setScreen(new MainScreen(ourGdxGame, ""));
				ourGdxGame.setScreen(new TestScreen(ourGdxGame));
			}
			return;
		}
		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();
		_batch.begin();
        _batch.setProjectionMatrix(_camera.combined);
        _batch.draw(imgBackground, 0, 0, GameConstants.DESKTOP_SCREEN_WIDTH, GameConstants.DESKTOP_SCREEN_HEIGHT);
        
        startGame.render(_batch);
        howPlayGame.render(_batch);
        quitGame.render(_batch);
        
        if (Gdx.app.getType() != ApplicationType.Android) {
			_mousePos.x = Gdx.input.getX();
			_mousePos.y = Gdx.input.getY();			
			_batch.draw(_imgMouse, _mousePos.x, _mousePos.y);
		}  
        _batch.end();        		
        //stage.draw();
        
	}

	@Override
	public void show() {
		Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
		//nameLabel = new Label("NAME: ", skin);	
		//nameText = new TextField("", skin);
		
		Table table = new Table();		
		stage.addActor(table);
		float width  = GameConstants.DESKTOP_SCREEN_WIDTH / 2.0f ;
		float height = GameConstants.DESKTOP_SCREEN_HEIGHT / 2.0f;		
		table.setSize(width,height);
		
		table.setPosition( GameConstants.DESKTOP_SCREEN_WIDTH/2 ,
						   0f);
		
		//table.add(nameLabel);
		//table.add(nameText).width(200);
		//table.row();
		
		TextButton startButton = new TextButton("Start Game", skin);
		startButton.addCaptureListener(new InputListener() {
			public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
				GameConfiguration.setInitialValues();
				
				isLoading = true;
				timeLoading = System.currentTimeMillis();	
				Skin skin = new Skin(Gdx.files.internal("data/uiskin2.json"));
				titleScreen = new Label("Loading ... ", skin);	
				
				stage = new Stage();
				Table table = new Table();	
				table.remove();
				stage.addActor(table);
				float width  = GameConstants.SCREEN_WIDTH / 2.0f ;
				float height = GameConstants.SCREEN_HEIGHT / 2.0f;		
				table.setSize(width, height);
				table.setPosition(GameConstants.DESKTOP_SCREEN_WIDTH/2.0f - width/2.0f,GameConstants.DESKTOP_SCREEN_HEIGHT/2.0f - height/2.0f);
				
				table.add(titleScreen);		
				table.row();
				table.center();
				//displayScreen();
				return true;				
			}
		});
		table.add(startButton).width(width).height(height/3).colspan(2);
		table.row();		
		
		TextButton howPlayButton = new TextButton("How to play?", skin);
		howPlayButton.addCaptureListener(new InputListener() {
			public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
				Vector<String> filePath = new Vector<String>();
				filePath.add("tutorial/obstacles.png");
				filePath.add("tutorial/spacetolisten.png");
				filePath.add("tutorial/zones.png");
				ourGdxGame.setScreen(new DisplayImageScreen(ourGdxGame, GameConstants.MENU_SCREEN,
						filePath, 6000));
				return true;				
			}
		});
		
		table.add(howPlayButton).width(width).height(height/3).colspan(2);
		table.row();
		
		TextButton exitButton = new TextButton("Exit", skin);
		exitButton.addCaptureListener(new InputListener() {
			public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
				System.exit(0);
				return true;				
			}
		});
		
		table.add(exitButton).width(width).height(height/3).colspan(2);
		table.row();		
		table.right();
		stage.addActor(table);
		
		table.left();
		
		_batch = new SpriteBatch();
		
		// Ortographic camera
		_camera = new OrthographicCamera(GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT);
		_camera.setToOrtho(true, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT);
		
		_assetManager = new AssetManager();
		_assetManager.load("data/handCursor.png", Texture.class);
		_assetManager.load("data/mainmenu.png", Texture.class);
		_assetManager.load("data/buttonBackground.png", Texture.class);
		_assetManager.load("data/buttonBackgroundPressed.png", Texture.class);
		_assetManager.finishLoading();
    	
		// Get assets
		_imgMouse = new TextureRegion(_assetManager.get("data/handCursor.png", Texture.class));
        _imgMouse.flip(false, true);
        Gdx.input.setCursorCatched(true);
    	_mousePos = new Vector3();

    	imgBackground = new TextureRegion(_assetManager.get("data/mainmenu.png", Texture.class));
    	imgBackground.flip(false, true);
    	
    	TextureRegion buttonBackground = new TextureRegion(_assetManager.get("data/buttonBackground.png", Texture.class));
		TextureRegion buttonBackgroundClicked = new TextureRegion(_assetManager.get("data/buttonBackgroundPressed.png", Texture.class));
			
		startGame.setBackground(buttonBackground);
		howPlayGame.setBackground(buttonBackground);
		quitGame.setBackground(buttonBackground);
		
		startGame.setBackgroundClicked(buttonBackgroundClicked);
		howPlayGame.setBackgroundClicked(buttonBackgroundClicked);
		quitGame.setBackgroundClicked(buttonBackgroundClicked);
		
		startGame.setFont(_fontText);
		howPlayGame.setFont(_fontText);
		quitGame.setFont(_fontText);
	}

	
	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}
	
}
